package com.whut.whw.util;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;

/**
 * <pre>
 *     author : 杨丽金
 *     time   : 2017/06/04
 *     desc   : 分享工具
 *     version: 1.0
 * </pre>
 */
public class ShareUtils {
    /**
     * 将text复制到剪贴板，ShareUtils.copyToClipBoard(this, webView.getUrl());
     * @param context：上下文
     * @param text：要复制的文字
     */
    public static void copyToClipBoard(Context context, String text) {
        ClipboardManager cm = (ClipboardManager) context.getSystemService(
                Context.CLIPBOARD_SERVICE);
        cm.setPrimaryClip(ClipData.newPlainText("xdroid_copy", text));
        Toast.makeText(context, "复制成功", Toast.LENGTH_SHORT).show();
    }

    /**
     * 将链接在浏览器中打开，ShareUtils.openInBrowser(this, webView.getUrl());
     * @param context：上下文
     * @param url：要打开的链接
     */
    public static void openInBrowser(Context context, String url) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        Uri uri = Uri.parse(url);
        intent.setData(uri);
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        } else {
            Toast.makeText(context, "打开失败了，没有可打开的应用", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 分享text，ShareUtils.shareText(this, webView.getTitle() + " " + webView.getUrl() + " 来自「XDroid」");
     * @param context：上下文
     * @param shareText：要分享的文字
     */
    public static void shareText(Context context, String shareText) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_SUBJECT, "分享");
        intent.putExtra(Intent.EXTRA_TEXT, shareText);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(Intent.createChooser(intent, "分享"));
    }
}
